/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.resources;

import java.io.InputStream;

import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.api.ILogEntry;
import net.sourceforge.perforce.core.api.IPerforceServer;
import net.sourceforge.perforce.core.syncinfo.ResourceSyncInfo;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.sync.IRemoteResource;

/**
  * @version $Revision: 46 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class RemoteFile extends RemoteResource
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Stores the depot filename. */
	private String depotFileName;
	/** Stores the revision. */
	private String revision;
	/** Stores the resource sync info. */
	private ResourceSyncInfo resourceSyncInfo;
	
	//-------------------------------------------------------------------------- Public Constructors
	
	/** Constructor for RemoteFile.
	  * 
	  * @param server the perforce server
	  * @param depotFileName the depot filename.
	  * @param revision the revision.
	  */
	public RemoteFile(IPerforceServer server, String depotFileName, String revision)
	{
		this.depotFileName = depotFileName;
		this.revision = revision;
		
		setServer(server);
	}

	// ------------------------------------------------------------------------------ Public Methods
	
	/** @see RemoteResource#find(String, IProgressMonitor) */
	public RemoteResource find(String name, IProgressMonitor monitor) 
		throws PerforceException 
	{
		if (name.equals(getDepotFilename()))
		{
			return this;
		}
		
		return null;
	}
	
	/** @see IRemoteResource#getContents(IProgressMonitor) */
	public InputStream getContents(IProgressMonitor progress) 
		throws PerforceException
	{
		return getServer().print(depotFileName, revision);
	}

	/** @see IRemoteResource#getName() */
	public String getName()
	{
		int start = depotFileName.lastIndexOf('/');
		return depotFileName.substring(start + 1);
	}

	/** @see IRemoteResource#isContainer() */
	public boolean isContainer()
	{
		return false;
	}

	/** @see RemoteResource#getSyncInfo() */
	public ResourceSyncInfo getSyncInfo(IProgressMonitor monitor)
	{
		try
		{
			if (resourceSyncInfo == null)
			{
				ResourceSyncInfo[] resourceSyncInfos = 
					getServer().fstat(depotFileName + revision, monitor);
				if (resourceSyncInfos.length == 1)
				{
					resourceSyncInfo = resourceSyncInfos[0];
				}
			}
				
			return resourceSyncInfo;
		}
		catch (PerforceException e)
		{
			PerforceCorePlugin.log(e.getStatus());
		}
		
		return null;
	}

	/** @see IRemoteResource#members(IProgressMonitor) */
	public IRemoteResource[] members(IProgressMonitor progress)
		throws PerforceException
	{
		return new IRemoteResource[0];
	}

	/** Returns the depot filename.
	  * 
	  * @return the depot file name.
	  */
	public String getDepotFilename()
	{
		return getSyncInfo(null).getDepotFileName();
	}

	/** @see IP4RemoteFile#getLogEntries(IProgressMonitor) */
	public ILogEntry[] getLogEntries(boolean followBranches, IProgressMonitor monitor)
		throws TeamException
	{	
		return getServer().filelog(this, 25, followBranches);
	}
	
	/** Checks whether the remote file is deleted.
	  * 
	  * @return true if remote is deleted.
	  */
	public boolean isDeleted()
	{
		return getSyncInfo(null).isDeleted();
	}
	
	/** Returns the revision of the file. 
	  *
	  * @return the revision as string.
	  */
	public String getRevision()
	{
		return revision;
	}
}
